﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    public class FootprintMesh : MonoBehaviour
    {
        private Mesh m_mesh;
        private Vector3[] vertices;
        private Vector3[] normals;
        private Vector2[] uv;
        private Vector2[] uv2;
        private int[] triangles;

        private int curIndex = 0;
        private int maxCount = 64;

        public void SetMaterial(int count,Material mat)
        {
            this.maxCount = count;
            Renderer renderer = GetComponent<Renderer>();
            if (renderer != null) renderer.sharedMaterial = mat;
            this.vertices = new Vector3[4 * count];
            this.normals = new Vector3[4 * count];
            this.uv = new Vector2[4 * count];
            this.uv2 = new Vector2[4 * count];
            this.triangles = new int[6 * count];
            if(m_mesh == null)
            {
                MeshFilter meshFilter = GetComponent<MeshFilter>();
                if(meshFilter.mesh == null)
                {
                    meshFilter.mesh = new Mesh();
                }
                this.m_mesh = meshFilter.mesh;
                this.m_mesh.name = "Footprints Mesh";
            }
        }

        public void CreatMesh(Vector3 pos, Vector3 fwd, Vector3 rht, Footprint footprint)
        {
            Vector2 size = footprint.Size;
            Vector3 groundOffset = Vector3.up* footprint.GroundOffset;
            int index = 4 * curIndex;
            Vector3 pos1 = pos + fwd * size.y * 0.5f - rht * size.x * 0.5f;
            vertices[index] = pos1 + groundOffset;
            normals[index] = Vector3.up;

            index = 4 * this.curIndex + 1;
            pos1 = pos + fwd * size.y * 0.5f + rht * size.x * 0.5f;
            vertices[index] = pos1 + groundOffset;
            normals[index] = Vector3.up;

            index = 4 * this.curIndex + 2;
            pos1 = pos - fwd * size.y * 0.5f - rht * size.x * 0.5f;
            vertices[index] = pos1 + groundOffset;
            normals[index] = Vector3.up;

            index = 4 * this.curIndex + 3;
            pos1 = pos - fwd * size.y * 0.5f + rht * size.x * 0.5f;
            vertices[index] = pos1 + groundOffset;
            normals[index] = Vector3.up;

            index = 4 * this.curIndex;
            uv[index] = footprint.UV1;
            uv[index+1] = footprint.UV2;
            uv[index+2] = footprint.UV3;
            uv[index+3] = footprint.UV4;

            Vector2 vertex = new Vector2(Time.timeSinceLevelLoad + footprint.Duration, footprint.Fadeout);
            uv2[index] = vertex;
            uv2[index + 1] = vertex;
            uv2[index + 2] = vertex;
            uv2[index + 3] = vertex;

            int index2 = 6 * this.curIndex;
            index = 4 * this.curIndex;
            triangles[index2] = index;
            triangles[index2 + 1] = index+1;
            triangles[index2 + 2] = index+2;
            triangles[index2 + 3] = index+2;
            triangles[index2 + 4] = index + 1;
            triangles[index2 + 5] = index + 3;

            this.curIndex++;
            if (this.curIndex >= this.maxCount) this.curIndex = 0;
            this.m_mesh.vertices = this.vertices;
            this.m_mesh.normals = this.normals;
            this.m_mesh.uv = this.uv;
            this.m_mesh.uv2 = this.uv2;
            this.m_mesh.triangles = this.triangles;
            this.m_mesh.RecalculateBounds();
        }
    }
}

